﻿using BaseUtils.Utils;
using NW_WEB_Service.com.app.entities;
using Service.src.com.utils.modal;
using Service.src.NW.Model;
using System.Web.Http;

namespace NW_WEB_Service.com.app.controllers
{

    [RoutePrefix("api/user-component")]
    public class UserComponentController : ApiController
    {
        /// <summary>
        /// 保存页面组件数据
        /// </summary>
        /// <param name="ComponentJson"></param>
        /// <returns></returns>
        [HttpPost()]
        [Route()]
        public HttpResult SaveUserPageComponent([FromBody] UserComponentEntity UP)
        {
            if (UP.ProjectId > 0)
            {
                FileHelper.WriteContent($"{AppData.RuntimeFlowDataViewDirectory}//{UP.ProjectId}//",
                    AppData.UserPage + UP.Type, UP.ComponentJson);
            }

            return new HttpResult().Success(UP);
        }

        /// <summary>
        /// 查看页面内的所有组件信息
        /// </summary>
        /// <param name="ComponentJson"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("list/{Id:int}")]
        public HttpResult GetUserComponent(int Id)
        {
            string Path = $"{AppData.RuntimeFlowDataViewDirectory + Id}//{AppData.UserPage}default.{AppData.UserPageComponent}";
            string ComponentJson = FileHelper.ReadContent(Path);
            return new HttpResult().Success(ComponentJson);
        }
    }
}
